from time import sleep

import pymysql
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# 定义关键字驱动类/工具类/基类/basePage
class WebKeys:
    # 构造方法,用于接收driver对象
    def __init__(self,driver):
        # driver=webdriver.Chrome()
        self.driver = driver
        # 定位器
        self.wait = WebDriverWait(self.driver,10)
    # 打开浏览器
    def open(self,url):
        self.driver.get(url)

    # 元素定位
    def locator(self,by,value):
        el = self.driver.find_element(by,value)
        # 将定位的元素框出来
        self.locator_station(el)
        sleep(2)
        return el

    # 元素定位+显示等待
    def locator_with_wait(self,by,value):
        # 定位器
        el_locator = (by,value)
        # 显示等待
        self.wait.until(EC.visibility_of_element_located(el_locator))
        return self.locator(by,value)

    # 显示定位地方，方便确认定位位置
    def locator_station(self,element):
        self.driver.execute_script(
            "arguments[0].setAttribute('style','arguments[1]')",
            element,
            "border:3px solid green" # 边框：绿色
        )

    # 窗口切换
    def change_window(self,n):
        # 获取句柄
        window_handles = self.driver.window_handles
        # 切换页签
        self.driver.switch_to.window(window_handles[n])

    # 鼠标按住不放,动态处理元素
    def mouse_click_and_hold(self,by,value):
        ActionChains(self.driver).click_and_hold(self.locator(by,value)).perform()

    # 输入文本，先清空，再输入，再回车
    def send_clear_enter(self,txt1,by,value,txt2):
        self.locator_with_wait(by, value).send_keys(txt1)
        self.locator_with_wait(by,value).clear()
        self.locator_with_wait(by, value).send_keys(txt2+Keys.ENTER)

    """
        select列表元素选择3中方法
    """
    # 下拉索引值
    def select_index(self,index,by,value):
        el = self.locator_with_wait(by,value)
        select = Select(el)
        select.select_by_index(index)

    # 下拉value属性值
    def select_value(self,by,value,txt):
        el = self.locator_with_wait(by,value)
        select = Select(el)
        select.select_by_value(txt)

    # 下拉文本值
    def select_text(self,by,value,txt):
        el = self.locator_with_wait(by,value)
        select = Select(el)
        select.select_by_visible_text(txt)

    # 滚动页面直到元素出现
    def scroll_to_element(self,by,value):
        # selenium4及以上版本才有scroll_to_element()这个方法
        # ActionChains(self.driver).scroll_to_element(self.locator_with_wait(by,value)).perform()
        # selenium4一下版本用
        self.driver.execute_script("arguments[0].scrollIntoView();",self.locator_with_wait(by,value))

    # 数据库检查
    def sql_check(self,sql):
        conn = pymysql.Connect(
            user='',
            password="",
            host=None,
            database=None,
            unix_socket=None,
            port=0,
            charset="",
        )
        cursor = conn.cursor()
        cursor.execute(query=sql)
        sql_data = cursor.fetchone()[0]
        conn.close()
        return sql_data